﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml.Serialization;

namespace AtlantisRevenge.GamePlay
{
    public class HiscoreData
    {
        public static string HighScoresFile = "highscores.dat";
        string PlayerName = "Digite o seu nome";

        [Serializable]
        public struct HighScoreData //Stuff for HighScoreData
        {
            public string[] PlayerName;
            public int[] Score;

            public int Count;

            public HighScoreData(int count)
            {
                PlayerName = new string[count];
                Score = new int[count];

                Count = count;
            }
        }

        public HiscoreData()
        {
            if (!File.Exists(HighScoresFile)) // Check to see if the save exists
            {
                //If the file doesn't exist, make a fake one...
                // Create the data to save
                HighScoreData data = new HighScoreData(5);
                data.PlayerName[0] = "Cleber";
                data.Score[0] = 2000;

                data.PlayerName[1] = "Eduardo";
                data.Score[1] = 1800;

                data.PlayerName[2] = "Pedro";
                data.Score[2] = 1500;

                data.PlayerName[3] = "Rafael";
                data.Score[3] = 1000;

                data.PlayerName[4] = "Lucas";
                data.Score[4] = 500;

                SaveHighScores(data, HighScoresFile);
            }
        }

        public static void SaveHighScores(HighScoreData data, string filename)
        {
            FileStream stream = File.Open(HighScoresFile, FileMode.Create); // Open the file, creating it if necessary
            XmlSerializer serializer = new XmlSerializer(typeof(HighScoreData));    // Convert the object to XML data and put it in the stream
            serializer.Serialize(stream, data);
            stream.Close(); // Close the file
        }


        public static HighScoreData LoadHighScores(string filename)
        {
            FileStream stream = File.Open(HighScoresFile, FileMode.Open, FileAccess.Read);    // Open the file
            XmlSerializer serializer = new XmlSerializer(typeof(HighScoreData));    // Convert the object to XML data and put it in the stream
            HighScoreData data = (HighScoreData)serializer.Deserialize(stream);     // Read the data from the file
            stream.Close(); // Close the file
            return (data);
        }


        /*
         * TODO: Use essa função para introduzir um novo highscore no jogo
         */
        private void NewHighScore() //Save player highscore when game ends
        {
            HighScoreData data = LoadHighScores(HighScoresFile);    // Create the data to saved

            int scoreIndex = -1;
            for (int i = data.Count - 1; i > -1; i--)
            {
                if (Main.Score >= data.Score[i])
                {
                    scoreIndex = i;
                }
            }

            if (scoreIndex > -1)    //New high score found ... do swaps
            {
                for (int i = data.Count - 1; i > scoreIndex; i--)
                {
                    data.PlayerName[i] = data.PlayerName[i - 1];
                    data.Score[i] = data.Score[i - 1];
                }

                data.PlayerName[scoreIndex] = PlayerName; //Retrieve User Name Here
                data.Score[scoreIndex] = Main.Score; // Retrieve score here

                SaveHighScores(data, HighScoresFile);
            }
        }


        public string makeHighScoreString() // Iterate through data if highscore is called and make the string to be saved
        {
            HighScoreData data = LoadHighScores(HighScoresFile);   // Create the data to save
            string scoreBoardString = "Recordes:\n\n";    // Create scoreBoardString

            for (int i = 0; i < 5; i++) // (10 means how many in the list/array/Counter)
            {
                scoreBoardString = scoreBoardString + data.PlayerName[i] + "-" + data.Score[i] + "\n";
            }
            return scoreBoardString;
        }
    }
}
